package gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.Timer;

public class ClockControlPanel extends JPanel implements ActionListener {
  private static final long serialVersionUID = -5822023900023271362L;

  private DateTimePanel pnlDateTime;
  private EarthPanel pnlEarth;
  private JSpinner spnSimTimeStep;
  private JButton btnStart;
  private ClockUpdateListener clockUpdater;
//  private int displayRate = Constants.DEFAULT_DISPLAY_RATE; //in milliseconds
//  private int simTimeStep; //in milliseconds
  private Timer dateTimeClock;
  private SimulationSettings settings;
  
  class Control {
    static final String start = "Start";
    static final String pause = "Pause";
    static final String reset = "Reset";
  }
  
  public ClockControlPanel(EarthPanel pnlEarth, SimulationSettings settings) {
    super();
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    this.pnlEarth = pnlEarth;
    this.settings = settings;
    
    pnlDateTime = new DateTimePanel();
    add(pnlDateTime);

    addControls();

    clockUpdater = new ClockUpdateListener(pnlDateTime, pnlEarth);
    dateTimeClock = new Timer(Constants.DEFAULT_DISPLAY_RATE, clockUpdater);    
  }
  
  public void adjustDisplayRate(int newRate) {
    dateTimeClock.setDelay(newRate);
  }
    
  private void addControls() {
    btnStart = new JButton(Control.start);
    btnStart.addActionListener(this);
    btnStart.setActionCommand(Control.start);
//    btnStart.setPreferredSize(new Dimension(80,20));
//    btnStart.setMinimumSize(new Dimension(80,20));
    
    JButton btnReset = new JButton(Control.reset);
    btnReset.addActionListener(this);
    btnReset.setActionCommand(Control.reset);
    
    JPanel pnlButtons = new JPanel();
    pnlButtons.setLayout(new BoxLayout(pnlButtons, BoxLayout.LINE_AXIS));
    pnlButtons.add(btnStart);
    pnlButtons.add(Box.createRigidArea(new Dimension(10,10)));
    pnlButtons.add(btnReset);
    
    add(pnlButtons);
  }
  
  public void actionPerformed(ActionEvent evt) {
    if(evt.getActionCommand().equals(Control.start)) {
      dateTimeClock.start();
      btnStart.setText(Control.pause);
      btnStart.setActionCommand(Control.pause);
      
      clockUpdater.setSimTimeStep(settings.getSimTimeStep());
    }
    if(evt.getActionCommand().equals(Control.pause)) {
      dateTimeClock.stop();
      btnStart.setText(Control.start);
      btnStart.setActionCommand(Control.start);
    }
    if(evt.getActionCommand().equals(Control.reset)) {
      dateTimeClock.stop();
      pnlDateTime.reset();
      pnlEarth.reset();
      
      btnStart.setText("Start");
      btnStart.setActionCommand(Control.start);
//      spnSimTimeStep.setEnabled(true);
    }    
  }
}
